package com.bw.servlet;

import com.bw.dao.UserDao;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/***************************
 FileAction:

 CreateTime:2022-05-09 8:21
 Author:Aaking-aline
 ****************************/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @SneakyThrows
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 登录操作就是 增删改查里的查询
        boolean b = new UserDao().selectUserByUsernameAndPassword(username, password);

        if(b){
            // 跳转到 成功页(也就是信息展示页)
            request.getRequestDispatcher("message.jsp").forward(request,response);
            return ;
        }
        // 跳转到 登录失败页(也就是回到登录页)
        request.setAttribute("error",new Object());
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
}
